<?php
include("config.php");

if(empty($_POST['token'])){
	echo json_res(true,'paramError','提交数据不全!');
	return;
}
//////////////////////////////////////////////////////////////////////////////////////
//获取课表
$snoopy1 = new Snoopy;
//模拟HEADER
$snoopy1->cookies['ASP.NET_SessionId'] = $_SESSION["ASP_NET_SessionId"];
//获取分析表单
$snoopy1->fetch($tableurl);
$res1 = $snoopy1->results;

//超时返回
if ($snoopy1->status != '200' || $snoopy1->timed_out) {
	echo json_res(true,'systemTimeOut',"页面获取超时！");
	return;
}

if(stripos($res1,"action=\"login.aspx\"")){
	echo json_res(true,'tokenTimeOut',"token失效");
	return;
}

$res1_html = new simple_html_dom();
$res1_html->load($res1);
$table_trs  = $res1_html->find('table.GridViewStyle tr');
$info_data = [];
$info_title= [];
foreach ($table_trs as $key => $value){
	if($key==0){
		$res1_tds = $value->find('th');
		foreach ($res1_tds as $td_key => $td){
			$info_title[] = trim(strip_tags($td->innertext));
			if($td_key == 6){
				$info_title[] = "开始周";
				$info_title[] = "结束周";
				$info_title[] = "星期";
				$info_title[] = "课节";
			}
		}
	}else{
		$res1_tds = $value->find('td');
		$res1_tds_data = [];
		foreach ($res1_tds as $td_key => $td){
			$res1_tds_data[] = trim(strip_tags($td->innertext));
			if($td_key == 6){
				$week_data = trim(strip_tags($td->innertext));
				$week_data_arr = explode(':', $week_data);
				$week_se_arr   = explode('-', str_replace(["第","周"],"",$week_data_arr[0]));
				$res1_tds_data[] = $week_se_arr[0];
				$res1_tds_data[] = $week_se_arr[1];
				$week_d_arr   = explode(' ', str_replace(["星期","上午","下午","晚上"],"",$week_data_arr[2]));
				$res1_tds_data[] = $week_d_arr[0];
				$res1_tds_data[] = $week_d_arr[1];
			}
		}
		$info_data[] = $res1_tds_data;
		if(count($week_d_arr)==4){
			$res1_tds_data[9]  = $week_d_arr[2];
			$res1_tds_data[10] = $week_d_arr[3];
			$info_data[] = $res1_tds_data;
		}
	}
}
if(isset($info_data[0][0]) && $info_data[0][0]=="没查询到相关内容")
$info_data = [];

$tableRow = ["title"=>$info_title,"data"=>$info_data];

$table["title"] = ["课程","教师","地点","开始周","结束周","星期","节次","类型"];
$week_hash = ["一"=>"1","二"=>"2","三"=>"3","四"=>"4","五"=>"5","六"=>"6","日"=>"7"];
$table["data"] = [];

foreach ($tableRow["data"] as $key => $value) {
	$table["data"][] = [
		"kc"  =>$value[4],
		"js"  =>$value[5],
		"dd"  =>$value[1],
		"ksz" =>$value[7],
		"jsz" =>$value[8],
		"xq"  =>$week_hash[$value[9]],
		"jc"  =>$value[10],
		"lx"  =>$value[13]=="是"?"学位课":"非学位课",
	];
}

$tableRow["campus_id"] = "3";
$table["row"] = $tableRow;

//////////////////////////////////////////////////////////////////////////////////////
echo json_res(false,$table);
return;

?>